var listPage = avalon.define({
	$id: "listPage",
	uid: "",
	name:'',
	receiptedPageNo: 1,
	receipted: [],
	noReceiptedData: "",
	receiptedTotal: 0
});

mui.ready(function() {
	// mui('#subAccountList').on('tap','.mui-btn', );
	var uid = $("#uid").val();
	listPage.uid = uid;
	
	if("" != uid) {
		mui.ajax('/user/get.json', {
			data: {
				uid: listPage.uid
			},
			dataType: 'json',
			success: function(data) {
				if(""!=data&&null!=data){
					listPage.name = data.name;
				}
			}
		});
	}
	$('#subAccountList').on('tap','.mui-btn',function(event){
		var self=this;
		var li = self.parentNode.parentNode;
		mui.confirm('确认删除该条记录？', '提示', ['确认', '取消'], function(e){
			if(e.index==0) {
				li.parentNode.removeChild(li);
				console.log($(li).attr('data-id'));
				delSubAccount($(li).attr('data-id'));
			}else {
				setTimeout(function(){
					mui.swipeoutClose(li);
				},0);
			}
		});
	});
	function delSubAccount(id) {
		mui.ajax('/user/del.json', {
			data: {
				uid: id
			},
			dataType: 'json',
			type: 'post',
			success: function(data) {
				if(""!=data&&null!=data&&data.status==1){
					
				}
			}
		});
	}
	mui.init({
		pullRefresh:{
			container:'#pullrefresh',
			down:{
				style:'circle',
				contentdown: $.i18n.prop('contentDown'),
				contentover: $.i18n.prop('contentOver'),
				contentrefresh: $.i18n.prop('contentRefresh'),
				contentnomore: $.i18n.prop('noData'),
				callback:pulldownRefresh
			},
			up:{
				auto:true,
				contentdown: $.i18n.prop('contentDown'),
				contentover: $.i18n.prop('contentOver'),
				contentrefresh: $.i18n.prop('contentRefresh'),
				contentnomore: $.i18n.prop('noData'),
				callback: pullupRefresh
			}
		}
	});
	// 上拉加载
	function pulldownRefresh(){
		var self = this;
		listPage.receiptedPageNo = 1;
		listPage.receipted = [];
		listPage.noReceiptedData = "";
		
		mui.ajax('/user/list.json', {
			data: {
				'customer.id': listPage.uid
			},
			dataType: 'json',
			type: 'post',
			success: function(data) {
				console.log(data);
				var records = data.records;
				listPage.receiptedTotal = data.totalCount;
				if(null == records || records.length == 0) {
					listPage.receipted = [];
					listPage.noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
				} else {
					listPage.receipted = records;
					listPage.noReceiptedData = "";
					listPage.receiptedPageNo = 2;
				}
				// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
				self.endPulldownToRefresh(false);
			}
		});
	}
	// 下拉刷新
	function pullupRefresh(){
		mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);//参数为true代表没有更多数据了.
		var self = this;
		listPage.noReceiptedData = "";
		
		mui.ajax('/user/list.json', {
			data: {
				"customer.id": listPage.uid,
				"pageNo": listPage.receiptedPageNo,
			},
			dataType: 'json',
			type: 'post',
			success: function(data) {
				console.log(data);
				var records = data.records;
				listPage.receiptedTotal = data.totalCount;
				if((listPage.receiptedPageNo <= data.totalPage) && (null != records)) {
					listPage.receipted = listPage.receipted.concat(records);
					listPage.receiptedPageNo++;
				} else {
					mui.toast("No More Data");
				}
				if(data.totalCount == 0) {
					listPage.noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
				} else {
					listPage.noReceiptedData = "";
				}
				// mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
				self.endPulldownToRefresh(false);
			}
		});
	}
});
